package com.viewplug.view.swap;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.PointF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.widget.ListView;
import android.widget.Scroller;

import com.viewplug.listView.SwapListViewItem;

/**
 * Created by mac_wk on 14-12-23.
 */
public class SwapListView extends ListView {
    public final static int SHOW = 1;
    public final static int HIDDEN = 2;
    public final static int SCROLL = 3;
    private VelocityTracker vTracker = null;

    public SwapListView(Context context) {
        this(context, null);

    }

    public SwapListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SwapListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SwapListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this(context, attrs, defStyleAttr);
    }
    private void initView() {
    }
    private SwapListViewContent nowPressView;
    private PointF mLastMove = new PointF();
    private int mTouchPosition = 0;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                int oldPos = mTouchPosition;
                mLastMove.set(ev.getX(), ev.getY());
                mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
                if (vTracker == null) {
                    vTracker = VelocityTracker.obtain();
                } else {
                    vTracker.clear();
                }
                vTracker.addMovement(ev);
                if (oldPos == mTouchPosition && nowPressView != null && nowPressView.getStatus() == SHOW) {
                    hidden();
                    return false;
                }
                SwapListViewContent newPressView = (SwapListViewContent) getChildAt(mTouchPosition - getFirstVisiblePosition());
                if (nowPressView != null && nowPressView.getStatus() == SHOW) {
                    hidden();
                    return false;
                }
                nowPressView = newPressView;

                break;
            case MotionEvent.ACTION_MOVE:
                if (nowPressView == null)//如果没有触摸任何view
                    break;
                vTracker.addMovement(ev);
                vTracker.computeCurrentVelocity(1000);
                int pressX = (int) ev.getX();
                int pressY = (int) ev.getY();
                if (pressX - mLastMove.x < -5&&Math.abs(pressY-mLastMove.y)<20 || nowPressView.getStatus() == SCROLL) {
                    int x = (int) (ev.getX() - mLastMove.x);
                    move(x);
                    nowPressView.setStatus(SCROLL);
                    //如果是右侧未滑动状态
                    mLastMove.set(ev.getX(), ev.getY());
                    return false;
                }
                mLastMove.set(ev.getX(), ev.getY());
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if (nowPressView != null) {
                    if (Math.abs(vTracker.getXVelocity()) > 400 && nowPressView.getStatus() == SCROLL)
                        scroller(true);
                    else
                        scroller(false);
                }
                releasevTracker();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }
    private void releasevTracker(){
        if (vTracker!=null)
        {
            vTracker.clear();
            vTracker.recycle();
            vTracker=null;
        }

    }
    private void hidden() {
        nowPressView.hidden();
    }
    private void scroller(boolean b) {
        nowPressView.scroller(b);
    }

    private void move(int x) {
        if (x == 0)
            return;
        if (nowPressView != null) {
            if (nowPressView.getStatus() != SCROLL) {
                nowPressView.setStatus(SCROLL);
            }
            nowPressView.move(-x);
            invalidate();
            postInvalidate();
        }
    }

    public int getStatus() {
        if (nowPressView != null)
            return nowPressView.getStatus();
        return HIDDEN;

    }
}
